package TestPark;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class ParkTest {

	RecordingDao rdao = new RecordingDao();
	ParkingSpaceDao pdao = new ParkingSpaceDao();
	Scanner sc = new Scanner(System.in);

	SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	// yyyy-MM-dd HH:mm:ss sss
	public static void main(String[] args) {
		ParkTest p = new ParkTest();
		p.create();
		p.start();
	}

	/**
	 * 开始界面
	 */
	void start() {
		while (true) {
			System.out.println("------------------------");
			System.out.println("请选择功能:");
			System.out.println("1、停车");
			System.out.println("2、取车");
			System.out.println("3、查看全部停车位的状态");
			System.out.println("4、查询全部收费记录");
			System.out.println("0、结束程序");
			System.out.println("------------------------");
			String str = sc.nextLine();
			switch (str) {
			case "1":
				parking();
				break;
			case "2":
				pickUp();
				break;
			case "3":
				checkAllStatus();
				break;
			case "4":
				checkCost();
				break;
			case "0":
				System.out.println("程序已结束");
				return;
			default:
				System.out.println("输入错误，请重新尝试：");
			}
		}
	}

	/**
	 * 创建停车位，停车位号1-20
	 */
	void create() {
		for (int i = 0; i < 20; i++) {
			ParkingSpaceModel ps = new ParkingSpaceModel();
			ps.setId(i + 1);
			ps.setStatus(0);
			pdao.insert(ps);
		}
	}

	/**
	 * 查询全部停车位的状态
	 */
	void checkAllStatus() {
		List<ParkingSpaceModel> spaceList = pdao.selectList();
		for (ParkingSpaceModel ps : spaceList) {
			System.out.println("停车位号：" + ps.getId());
			System.out.println("停车位状态：" + statusback(ps));
		}
	}

	/**
	 * 判断停车位的状态
	 * 
	 * @param ps
	 * @return
	 */
	String statusback(ParkingSpaceModel ps) {
		if (ps.getStatus() == 0) {
			return "空闲";
		} else
			return "已占用";
	}

	/**
	 * 遍历空闲的停车位
	 */
	void checkStatus() {
		List<ParkingSpaceModel> spaceList = pdao.selectList();
		for (ParkingSpaceModel ps : spaceList) {
			if (ps.getStatus() == 0) {
				System.out.println("停车位号：" + ps.getId() + "\t现空闲状态");
			}
		}
	}

	/**
	 * 查询全部收费记录
	 */
	void checkCost() {
		List<RecordingModel> recordingList = rdao.selectList();
		for (RecordingModel rec : recordingList) {
			Timestamp str1date = rec.getBefDate();
			Timestamp str2date = rec.getAftDate();
			System.out.println("停车日期：" + str1date);
			System.out.println("取车日期：" + str2date);
			System.out.println("停车位号：" + rec.getId());
			System.out.println("停车车牌号：" + rec.getCarId());
			BigDecimal t = new BigDecimal(rec.getFare()).setScale(2, BigDecimal.ROUND_HALF_UP);
			System.out.println("总共收取停车费为：" + t);
		}
	}

	/**
	 * 停车
	 */
	void parking() {
		int id = 0;
		System.out.println("友情提示：每小时收费两元！");
		System.out.println("请输入车牌号以及车身高度（本停车场限高3米）");
		System.out.println("请输入车牌号：");
		String cid = sc.nextLine();
		System.out.println("请输入车身高度：");
		double high = Double.parseDouble(sc.nextLine());
		if (high > 3.0) {
			System.out.println("抱歉，你的车身高度超过本停车场的限高，您无法在此停车场停车！");
		} else {
			System.out.println("请查看现有的停车位");
			checkStatus();
			System.out.println("请输入想要停车的停车位号：");
			id = Integer.parseInt(sc.nextLine());
			ParkingSpaceModel model = new ParkingSpaceModel();
			model.setId(id);
			model.setStatus(1);
			int result = pdao.update(model);
			if (result == 1) {
				System.out.println("已完成停车记录，请前往停车场指定车位停车！");
			}
		}
		RecordingModel rec = new RecordingModel();
		Date d = new Date();
		Timestamp date = new Timestamp(d.getTime());
		rec.setBefDate(date);
		rec.setId(id);
		rec.setCarId(cid);
		rdao.insert(rec);
	}

	/**
	 * 取车
	 */
	void pickUp() {
		List<RecordingModel> recordingList = rdao.selectList();
		System.out.println("请输入停车位：");
		int id = Integer.parseInt(sc.nextLine());
		System.out.println("请输入车牌号：");
		String cid = sc.nextLine();
		Timestamp beforetime = null;
		RecordingModel rec = new RecordingModel();
		rec.setId(id);
		rec.setCarId(cid);
		for(RecordingModel re : recordingList) {
			if(id==re.getId()&&cid.equals(re.getCarId())) {
				beforetime = re.getBefDate();// 停车时间
				break;
			}
		}
		
		Date d = new Date();
		Timestamp aftertime = new Timestamp(d.getTime());// 取车时间
		long t1 = beforetime.getTime(); // 毫秒数
		long t2 = aftertime.getTime();
		BigDecimal t3 = new BigDecimal(t2 - t1);
		BigDecimal t4 = new BigDecimal(1000 * 60 * 60);
		BigDecimal tim = t3.divide(t4, 6, BigDecimal.ROUND_UP);
		double f = tim.doubleValue();
		double time = Math.ceil(f);
		double fare = time * 2.0; // 费用

		rec.setAftDate(aftertime);
		rec.setFare(fare);

		int result = rdao.update(rec);
		if (result == 1) {
			System.out.println("取车成功，您需要缴纳的停车费为：" + fare + ",请及时缴费！");
		}
	}
}
